package cn.chaohi.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 配置文件读取工具类
 * 
 * @author bobo
 *
 */
public class ConfigUtils {

	/**
	 * 日志
	 */
	private static Logger logger = Logger.getLogger(ConfigUtils.class);
	/**
	 * 属性配置类
	 */
	private static Properties p = new Properties();
	/**
	 * 配置文件根路径
	 */
	private static final String CONF_BASE=System.getProperty("cfgbase");
	/**
	 * 配置文件地址
	 */
	private static final String CONF_PATH =CONF_BASE+File.separator+"chaohi.properties";
	//private static final String CONF_PATH = "D:/chaohi2.0/market_chaohi.properties";

	/**
	 * OSS 访问域名
	 */
	public static final String OSS_CHAOHI_ENDPOINT;
	/**
	 * OSS 密钥
	 */
	public static final String OSS_CHAOHI_ACCESSKEYID;
	/**
	 * OSS 会话ID
	 */
	public static final String OSS_CHAOHI_ACCESSKEYSECRET;
	/**
	 * OSS db文件存储包
	 */
	public static final String OSS_DB_BUCKETNAME;
	/**
	 * OSS 商品信息操作包(多点文件商品.zip上传目录)
	 */
	public static final String OSS_DMALL_BUCKETNAME;
	/**
	 * 多点数据存储目录
	 */
	public static final String OSS_DMALL_REMOTE_URL;
	/**
	 * redis 数据存储包
	 */
	public static final String REDIS_PACKET;
	/**
	 * 超市信息xml配置文件路径
	 */
	public static final String MARKET_XML_PATH=CONF_BASE+File.separator+"market_data.xml";
	/**
	 * 服务器配置
	 */
	public static final String SERVER_IP;
	/**
	 * OSS STS 获取授权key id
	 */
	public static final String OSS_STS_ACCESS_KEYID;
	/**
	 * OSS STS 获取授权key 秘钥
	 */
	public static final String OSS_STS_ACCESS_KEYSECRET;
	/**
	 * OSS STS 权限
	 */
	public static final String OSS_STS_ROLE_ARN;
	/**
	 * OSS 资源文件bucket
	 */
	public static final String OSS_RESOURCE_BUCKET;
	/**
	 * OSS 公开资源文件bucket
	 */
	public static final String OSS_PUBLIC_RESOURCE_BUCKET;
	/**
	 * OSS STS 权限缓存失效时长 单位:s
	 */
	public static final String OSS_STS_REDIS_LIVE_TIME;
	
	/**
	 * OLE API interface
	 */
	public static final String OLE_GET_LOGIN_QRCODE_URL;
	
	/**
	 * OLE API Keys
	 */
	public static final String OLE_GET_KEYS;
	
	/**
	 * Configuration of applet get user info from wechat server.
	 */
	public static final String APPLET_SERVER_PATH;
	public static final String APPLET_APPID;
	public static final String APPLET_APPSECRET;
	
	/**
	 * 读取properties配置文件信息
	 */
	static {
		try {
			p.load(new FileInputStream(new File(CONF_PATH)));
		} catch (IOException e) {
			logger.error("初始化配置文件【conf/conf.properties】发生异常:" + e.getMessage());
		}
		//服务器主机
		SERVER_IP = p.getProperty("market.server.ip");
		/*
		 * OSS 配置
		 */
		OSS_CHAOHI_ENDPOINT = p.getProperty("oss.chaohi.endpoint");
		OSS_CHAOHI_ACCESSKEYID = p.getProperty("oss.chaohi.accesskeyid");
		OSS_CHAOHI_ACCESSKEYSECRET = p.getProperty("oss.chaohi.accesskeysecret");
		//多点数据存储目录
		OSS_DMALL_REMOTE_URL = p.getProperty("oss.dmall.remote.url");
		OSS_DMALL_BUCKETNAME = p.getProperty("oss.dmall.bucketname");
		//db文件存储包
		OSS_DB_BUCKETNAME = p.getProperty("oss.db.bucketname");
		/*
		 * redis 配置
		 */
		REDIS_PACKET = p.getProperty("redis.packet");
		/*
		 * OSS 获取授权
		 */
		OSS_STS_ACCESS_KEYID = p.getProperty("oss.sts.accesskeyid");
		OSS_STS_ACCESS_KEYSECRET = p.getProperty("oss.sts.accesskeysecret");
		OSS_STS_ROLE_ARN = p.getProperty("oss.sts.rolearn");
		OSS_RESOURCE_BUCKET = p.getProperty("oss.resource.bucket");
		OSS_PUBLIC_RESOURCE_BUCKET = p.getProperty("oss.public.resource.bucket");
		OSS_STS_REDIS_LIVE_TIME = p.getProperty("oss.sts.redis.livetime", "3000");
		OLE_GET_LOGIN_QRCODE_URL = p.getProperty("ole.auth.server.base");
		OLE_GET_KEYS = p.getProperty("ole.auth.keys");
		
		//小程序的配置
		APPLET_SERVER_PATH = p.getProperty("applet.open.server.base");
		APPLET_APPID = p.getProperty("applet.open.appid");
		APPLET_APPSECRET = p.getProperty("applet.open.appsecret");
	}

	/**
	 * 根据key得到value的值
	 */
	public static String getValue(String key) {
		return p.getProperty(key);
	}
}
